/**
 * @author liangxi
 * @since 2024/3/25
 */

#include "../../../Header/Platform/Helper/LogHelper.h"

#ifdef __linux__
#include <ctime>
#endif

#ifdef _WIN32

#include <Windows.h>

std::string LogHelper::utf8ToGbk(const std::string &utf8_str) {
    int len = MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, nullptr, 0);
    wchar_t *wbuffer = new wchar_t[len];
    MultiByteToWideChar(CP_UTF8, 0, utf8_str.c_str(), -1, wbuffer, len);

    len = WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, nullptr, 0, nullptr, nullptr);
    char *buffer = new char[len];
    WideCharToMultiByte(CP_ACP, 0, wbuffer, -1, buffer, len, nullptr, nullptr);

    std::string gbk_str(buffer);

    delete[] wbuffer;
    delete[] buffer;

    return gbk_str;
}

#endif

Time LogHelper::getLocalTime() {
    int year, month, day, hour, minute, second;

#ifdef _WIN32
    SYSTEMTIME sys;
    GetLocalTime(&sys);
    year = sys.wYear;
    month = sys.wMonth;
    day = sys.wDay;
    hour = sys.wHour;
    minute = sys.wMinute;
    second = sys.wSecond;
#endif

#ifdef __linux__
    auto now = std::time(nullptr);
    auto nowST = std::localtime(&now);
    year = nowST->tm_year + 1900;
    month = nowST->tm_mon + 1;
    day = nowST->tm_mday;
    hour = nowST->tm_hour;
    minute = nowST->tm_min;
    second = nowST->tm_sec;
#endif

    return Time(year, month, day, hour, minute, second);
}
